package crew.blt.handsound.client.webgl;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;

import crew.blt.handsound.client.webgl.element.Element;
import crew.blt.handsound.client.webgl.element.ElementChangeHandler;
import crew.blt.handsound.client.webgl.property.Property;

public class Scene extends Composite implements ElementChangeHandler {

	private String id = "theScene";
	private FlowPanel panelElements = new FlowPanel();
	private Set<Element> elements = new HashSet<Element>();

	public Scene() {
		DecoratorPanel dec = new DecoratorPanel();
		dec.setWidget(panelElements);
		initWidget(dec);
	}
	
	/**
	 * Get the collection of elements into your scene.
	 * @return the elements
	 */
	public Set<Element> getElements() {
		return elements;
	}
	
	/**
	 * Add an element on the scene manipulation panel.
	 * @param el
	 * @return true if add success
	 */
	public boolean addElement(Element el){
		boolean success = elements.add(el);
		if(success){
			el.setHandler(this);
			panelElements.add(el);
		}
		return success;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public void onElementChange(Element el, Property<?, ?> newValue) {
		fireNativeSceneRefresh(getId());
	}

	private native void fireNativeSceneRefresh(String sceneId) /*-{ 
        $wnd.SceneJS.withNode(sceneId).render();
    }-*/; 
}
